package com.agileai.bpm.controller.emulation;

import com.agileai.bpm.bizmoduler.emulation.WfEmulationManage;
import com.agileai.bpm.service.running.ProcessEngine;
import com.agileai.bpm.service.running.RunningException;
import com.agileai.domain.DataParam;
import com.agileai.hotweb.controller.core.StandardListHandler;
import com.agileai.hotweb.renders.DispatchRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.StringUtil;

public class WfEmulationManageListHandler extends StandardListHandler{
	public WfEmulationManageListHandler(){
		super();
		this.editHandlerClazz = WfEmulationManageEditHandler.class;
		this.serviceId = buildServiceId(WfEmulationManage.class);
	}
	public ViewRenderer doDeleteAction(DataParam param) {
		
		String bizRecordId = param.get("bizRecordId");
		String processURI = param.get("WFP_ID");
		param.put("WFE_ID",bizRecordId);
		getService().deletRecord(param);
		
		param.remove("WFP_ID");
		storeParam(param);
		ProcessEngine processEngine = (ProcessEngine)this.lookupService("processEngine");
		try {
			processEngine.deleteInstanceAndHistorys(processURI,bizRecordId);
		} catch (RunningException e) {
			e.printStackTrace();
		}
		return new RedirectRenderer(getHandlerURL(getClass()));
	}
	public ViewRenderer doQueryAction(DataParam param){
		param.put("WFP_ID",param.get("CUR_WFP_ID"));
		return prepareDisplay(param);
	}
	protected void processPageAttributes(DataParam param) {
		if (!StringUtil.isNullOrEmpty(param.get("WFP_ID"))){
			this.setAttribute("CUR_WFP_ID", param.get("WFP_ID"));
		}
		if (!StringUtil.isNullOrEmpty(param.get("WFP_NAME"))){
			this.setAttribute("CUR_WFP_NAME", param.get("WFP_NAME"));
		}
	}
	protected void initParameters(DataParam param) {
		if (!StringUtil.isNullOrEmpty(param.get("WFP_ID"))){
			param.put("CUR_WFP_ID", param.get("WFP_ID"));
		}
		if (!StringUtil.isNullOrEmpty(param.get("WFP_NAME"))){
			param.put("CUR_WFP_NAME", param.get("WFP_NAME"));
		}		
	}
	public ViewRenderer doViewDetailAction(DataParam param){
		storeParam(param);
		return new DispatchRenderer(getHandlerURL(WfEmulationManageDetailHandler.class));
	}	
	protected WfEmulationManage getService() {
		return (WfEmulationManage)this.lookupService(this.getServiceId());
	}
}
